<?php
namespace app\admin\controller;

use app\admin\tool\Parser;
use app\common\model\Admin as AdminModel;
use think\Request;
use think\Loader;
use app\common\model\Role as RoleModel;

class Market extends Base{

    public function index(Request $request){
        $param = $request->param();

        $where = ['role'=>3];
        if(!empty($param['username'])) $where['username'] = ['like',"%".$param['username']."%"];
        if(!empty($param['nickname'])) $where['nickname'] = ['like',"%".$param['nickname']."%"];
        if(!empty($param['department'])) $where['department'] = $param['department'];

        $page = AdminModel::where($where)->paginate(10,false,['query'=>$where]);

        $this->data['model'] = $page;
        $this->data['column'] = $this->column();
        $this->data['title'] = "推广账号管理";
        $this->data['parser'] = new Parser();
        $this->data['query'] = $param;
        return view('',$this->data);
    }


    public function add(){
        $this->data['title'] = '用户添加';
        $this->data['roles'] = RoleModel::all();
        return view('',$this->data);
    }


    public function edit(Request $request){
        $this->data['title'] = '用户更新';
        $this->data['model'] = AdminModel::get($request->param('id'));
        $this->data['roles'] = RoleModel::all();
        return view('',$this->data);
    }


    public function handle(Request $request){

        $data = $request->post();

        $validate = Loader::validate('Admin');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['password'] = AdminModel::encrypt($data['password']);
        $data['update_time'] = time();

        if(empty($data['id'])){
            $data['add_time'] = $data['update_time'];
            if(AdminModel::create($data)){
                $this->success("新增成功");
            }
        }else{
            $navigationModel = new AdminModel();
            if($navigationModel -> save($data,['id'=>$data['id']])){
                $this->success("更新成功");
            }
        }

        $this->error("操作失败");
    }

    /**
     * 创建和更新分开方便权限管理
     * @param Request $request
     */
    public function create(Request $request){

        $data = $request->post();

        $validate = Loader::validate('Admin');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['password'] = AdminModel::encrypt($data['password']);
        $data['add_time'] = $data['update_time'] = time();
        $data['invitecode'] = $this->geneUniqueCode();

        if(AdminModel::create($data)){
            $this->success("新增成功");
        }

        $this->error("操作失败");

    }


    /**
     * @param Request $request
     */
    public function update(Request $request){
        $data = $request->post();

        if(empty($data['id'])){
            $this->error("操作失败");
        }

        $validate = Loader::validate('Admin');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['password'] = AdminModel::encrypt($data['password']);
        $data['update_time'] = time();

        $navigationModel = new AdminModel();
        if($navigationModel -> save($data,['id'=>$data['id']])){
            $this->success("更新成功");
        }

        $this->error("操作失败");
    }


    public function delete(Request $request){
        $model = AdminModel::get($request->param('id'));
        if($model){
            $res = $model ->delete();
            if($res){
                $this->success("删除成功");
            }
        }

        $this->error("操作失败");
    }



    protected function column(){
        return [
            'username'  =>['name'=>'登录账号(手机)','fun'=>'text','options'=>''],
            'nickname'  =>['name'=>'姓名','fun'=>'text','options'=>''],
            'department'=>['name'=>'部门','fun'=>'text','options'=>''],
            'invitecode'  =>['name'=>'邀请码','fun'=>'text','options'=>'']
        ];
    }


    /**
     * 产生唯一邀请码
     */
    protected function geneUniqueCode(){
        $code = make_random_str(8,"number");
        $exists = AdminModel::where(['invitecode'=>$code])->count();
        if($exists){
            return $this->geneUniqueCode();
        }
        return $code;
    }


}